package com.fable.biz.operate.impl;

import com.fable.biz.operate.CustomerSourceService;
import com.fable.biz.operate.dto.CustomerSourceDTO;
import com.fable.biz.operate.query.CustomerSourceQueryCondition;
import com.fable.dal.mapper.CustomerSourceMapper;
import com.fable.dal.model.CustomerSourceDO;
import com.fable.dal.model.CustomerSourceQuery;
import com.fable.utils.CalendarUtil;
import com.fable.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName CustomerSourceServiceImpl
 * @Description TODO
 * @Author aaters
 * @Date 2018/7/24 下午7:43
 * @Version 1.0
 **/
@Service("customerSourceService")
public class CustomerSourceServiceImpl implements CustomerSourceService {


    private static final Logger LOG = LoggerFactory.getLogger(CustomerSourceServiceImpl.class);

    @Autowired
    private CustomerSourceMapper customerSourceMapper;

    @Override
    public List<CustomerSourceDTO> querySourceAll(CustomerSourceQueryCondition condition) {
        try {
            CustomerSourceQuery customerSourceQuery = new CustomerSourceQuery();
            CustomerSourceQuery.Criteria queryCriteria = customerSourceQuery.createCriteria();
            if (condition != null) {
                if (StringUtil.isNotBlank(condition.getSourceName())) {
                    queryCriteria.andSourceNameLike("%" + condition.getSourceName() + "%");
                }
                if (StringUtil.isNotBlank(condition.getStartTime())) {
                    String startTime = condition.getStartTime();
                    Date time = CalendarUtil.parseString2Date(startTime, CalendarUtil.TIME_PATTERN);
                    queryCriteria.andGmtCreateGreaterThanOrEqualTo(time);
                }
                if (StringUtil.isNotBlank(condition.getEndTime())) {
                    String endTime = condition.getEndTime();
                    Date time = CalendarUtil.parseString2Date(endTime, CalendarUtil.TIME_PATTERN);
                    queryCriteria.andGmtCreateLessThanOrEqualTo(time);
                }
            }
            customerSourceQuery.setOrderByClause("id desc ");
            List<CustomerSourceDO> list = customerSourceMapper.selectByQuery(customerSourceQuery);
            List<CustomerSourceDTO> doList = new ArrayList<>();

            list.stream().filter(Objects::nonNull).forEach(customerSourceDO -> {
                CustomerSourceDTO customerSourceDTO = new CustomerSourceDTO();
                BeanUtils.copyProperties(customerSourceDO, customerSourceDTO);
                doList.add(customerSourceDTO);
            });

            LOG.info("获取客户类型成功:{}", doList);
            return doList;
        } catch (Exception e) {
            LOG.info("获取客户类型失败:{}", e.getLocalizedMessage());
        }
        return null;
    }

    @Override
    public CustomerSourceDO querySourceById(Integer id) {
        return customerSourceMapper.selectByPrimaryKey(id);
    }

    @Override
    public int addOrModifyCustomerSource(CustomerSourceDTO customerSourceDTO) {
        int count = 0;
        try {
            CustomerSourceDO customerSourceDO = customerSourceMapper.selectByPrimaryKey(customerSourceDTO.getId());
            if (customerSourceDO == null) {
                customerSourceDO = new CustomerSourceDO();
                customerSourceDO.setSourceName(customerSourceDTO.getSourceName());
                customerSourceDO.setSourceDescribe(customerSourceDTO.getSourceDescribe());
                customerSourceDO.setGmtCreate(new Date());
                customerSourceDO.setGmtModified(new Date());
                count = customerSourceMapper.insertSelective(customerSourceDO);
            } else {
                BeanUtils.copyProperties(customerSourceDTO, customerSourceDO);
                customerSourceDO.setGmtModified(new Date());
                count = customerSourceMapper.updateByPrimaryKeySelective(customerSourceDO);
            }
        } catch (Exception e) {
            LOG.info("添加或修改客户来源失败:{}", e.getLocalizedMessage());
        }
        return count;
    }
}
